package com.atguigu.controller;

import com.atguigu.entity.Admin;
import com.atguigu.entity.HouseBroker;
import com.atguigu.service.AdminService;
import com.atguigu.service.HouseBrokerService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import sun.dc.pr.PRError;

import java.lang.reflect.Array;
import java.util.Iterator;
import java.util.List;

@Controller
@RequestMapping("/houseBroker")
public class HouseBrokerController {

    @DubboReference
    private HouseBrokerService houseBrokerService;
    @DubboReference
    private AdminService adminService;

    @RequestMapping("/create")
    public String create(@RequestParam("houseId") Long houseId, ModelMap modelMap){
        //将全部的管理员查出来
        List<Admin> adminList = adminService.findAll();
        //本房源的所有经纪人的id号
        List<Long> brokerId = houseBrokerService.getBrokerId(houseId);

        for (int i = adminList.size() - 1; i >= 0; i--) {
            for (int i1 = brokerId.size() - 1; i1 >= 0; i1--) {
                int adminid = adminList.get(i).getId().intValue();
                if(adminid == brokerId.get(i1).intValue()) {
                    adminList.remove(i);
                    break;
                }
            }
        }
/*
        //遍历本房源的所有经纪人
        Iterator<Admin> iterator = adminList.iterator();
        while (iterator.hasNext()){
            for (int i = brokerId.size() - 1; i >= 0; i--) {
                long brokerid = iterator.next().getId().intValue();
                if(brokerId.get(i) == brokerid){
                    adminList.remove(iterator.next().getId());
                }
            }

        }
*/

        modelMap.addAttribute("adminList",adminList);
        modelMap.addAttribute("houseId",houseId);
        return "houseBroker/create";
    }


    @RequestMapping("/save")
    public String save(HouseBroker houseBroker){
        Admin admin = adminService.getById(houseBroker.getBrokerId());
        houseBroker.setBrokerName(admin.getName());
        houseBroker.setBrokerHeadUrl(admin.getHeadUrl());
        houseBrokerService.index(houseBroker);
        return "common/successPage";
    }
    @RequestMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id,ModelMap modelMap){
        HouseBroker houseBroker = houseBrokerService.getById(id);
        modelMap.addAttribute("houseBroker",houseBroker);
        List<Admin> adminList = adminService.findAll();
        modelMap.addAttribute("adminList",adminList);
        return "houseBroker/edit";
    }
@RequestMapping("/update")
    public String update(HouseBroker houseBroker){
    Admin admin = adminService.getById(houseBroker.getBrokerId());
    houseBroker.setBrokerName(admin.getName());
    houseBroker.setBrokerHeadUrl(admin.getHeadUrl());
    houseBrokerService.update(houseBroker);
    return "common/successPage";
}
@RequestMapping("/delete/{houseId}/{id}")
    public String delete(@PathVariable("houseId") Long houseId,@PathVariable("id") int id){
        houseBrokerService.delete(id);
        return "redirect:/house/"+houseId;
}
}
